<?php
/**
 * 类名称
 * User: 13651
 * Time: 2025/9/18 15:17
 */

namespace App\Repository;

use App\Models\LeshuaUnionAreaCode;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class LeshuaUionAreaCodeRepo extends CommonRepo
{
    /**
     * 数据入库
     * @param array $data
     * @param int $id
     * @return bool
     */
    public function saveData(array $data,int $id=0)
    {
        if ($id) {
            $result  = LeshuaUnionAreaCode::query()->find($id);
        } else {
            $result = new LeshuaUnionAreaCode();
        }
        if (empty($result))
            return 0;
        collect($data)->map(function ($val, $field) use ($result) {
            $result->{$field} = $val;
        });
        return $result->save() ? $result->id : 0;
    }

    /**
     * 获取列表数据
     * @param array $where
     * @return Builder[]|Collection
     */
    public function getParentCodeList(array $where)
    {
        $query = LeshuaUnionAreaCode::query();
        if (!empty($where['level']))
            $query->where('area_level',$where['level']);
        return $query->orderBy('id','asc');
    }

    /**
     * 获取指定条件数据
     * @param array $where
     * @return Builder[]|Collection
     */
    public function getByWhere(array $where)
    {
        $query = LeshuaUnionAreaCode::query();
        if (!empty($where['area_name']))
            $query->whereIn('area_name',$where['area_name']);
        if (!empty($where['area_code']))
            $query->whereIn('area_code',$where['area_code']);
        return $query->get();
    }
}